(function ($) {
	//加载完毕执行
	// load_doc = function () {
		// let baseUrl = "http://localhost:4000/saohexo/"
		var baseUrl = "https://tianqicha.gitee.io/saohexo/";
		// Search
		var $searchWrap = $('#search-form-wrap'),
			isSearchAnim = false,
			searchAnimDuration = 200;

		var startSearchAnim = function () {
			isSearchAnim = true;
		};

		var stopSearchAnim = function (callback) {
			setTimeout(function () {
				isSearchAnim = false;
				callback && callback();
			}, searchAnimDuration);
		};

		$('#nav-search-btn').on('click', function () {
			if (isSearchAnim) return;

			startSearchAnim();
			$searchWrap.addClass('on');
			stopSearchAnim(function () {
				$('.search-form-input').focus();
			});
		});

		$('.search-form-input').on('blur', function () {
			startSearchAnim();
			setTimeout(() => {
				$searchWrap.removeClass('on');
			}, 200);
			// $searchWrap.removeClass('on');
			stopSearchAnim();
		});

		var data = $.ajax({ url: baseUrl + "content.json", async: false });

		var datarray = data.responseJSON;

		var search_data = [];

		$('.search-form-input').bind("input propertychange", function () {
			$('.search-data-box').removeClass('hide-search')
			search_data = []
			if ($('.search-form-input').val()) {
				datarray.forEach(e => {
					let a = e.title.indexOf($('.search-form-input').val()) != -1
					if (e.title.indexOf($('.search-form-input').val()) != -1) {
						if (search_data.length <= 5) {
							search_data.push(e)
						}
					}
				});
				$('.search-data-box').html("")
				search_data.forEach(e => {
					let url = baseUrl + e.path
					$('.search-data-box').append('<div class="search-box-item"><a href="' + url + '">' + e.title + '</a></div>')
				})
			}else{
				$('.search-data-box').html('未查询到结果!')
			}

		})
		//气泡
		$('.header').circleMagic({
			elem: '.header',
			radius: 35,
			densety: .2,
			color: 'rgba(255,255,255, .4)',
			// color: 'black',
			clearOffset: .2
		});

		// Share
		$('body').on('click', function () {
			$('.article-share-box.on').removeClass('on');
		}).on('click', '.article-share-link', function (e) {
			e.stopPropagation();

			var $this = $(this),
				url = $this.attr('data-url'),
				encodedUrl = encodeURIComponent(url),
				id = 'article-share-box-' + $this.attr('data-id'),
				offset = $this.offset();

			if ($('#' + id).length) {
				var box = $('#' + id);

				if (box.hasClass('on')) {
					box.removeClass('on');
					return;
				}
			} else {
				var html = [
					'<div id="' + id + '" class="article-share-box">',
					'<input class="article-share-input" value="' + url + '">',
					'<div class="article-share-links">',
					'<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
					'<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
					'<a href="http://pinterest.com/pin/create/button/?url=' + encodedUrl + '" class="article-share-pinterest" target="_blank" title="Pinterest"></a>',
					'<a href="https://plus.google.com/share?url=' + encodedUrl + '" class="article-share-google" target="_blank" title="Google+"></a>',
					'</div>',
					'</div>'
				].join('');

				var box = $(html);

				$('body').append(box);
			}

			$('.article-share-box.on').hide();

			box.css({
				top: offset.top + 25,
				left: offset.left
			}).addClass('on');
		}).on('click', '.article-share-box', function (e) {
			e.stopPropagation();
		}).on('click', '.article-share-box-input', function () {
			$(this).select();
		}).on('click', '.article-share-box-link', function (e) {
			e.preventDefault();
			e.stopPropagation();

			window.open(this.href, 'article-share-box-window-' + Date.now(), 'width=500,height=450');
		});

		// Caption
		$('.article-entry').each(function (i) {
			$(this).find('img').each(function () {
				if ($(this).parent().hasClass('fancybox')) return;

				var alt = this.alt;

				if (alt) $(this).after('<span class="caption">' + alt + '</span>');

				$(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
			});

			$(this).find('.fancybox').each(function () {
				$(this).attr('rel', 'article' + i);
			});
		});

		if ($.fancybox) {
			$('.fancybox').fancybox();
		}

		// Mobile nav
		var $container = $('#container'),
			isMobileNavAnim = false,
			mobileNavAnimDuration = 200;

		var startMobileNavAnim = function () {
			isMobileNavAnim = true;
		};

		var stopMobileNavAnim = function () {
			setTimeout(function () {
				isMobileNavAnim = false;
			}, mobileNavAnimDuration);
		}

		$('#main-nav-toggle').on('click', function () {
			if (isMobileNavAnim) return;

			startMobileNavAnim();
			$container.toggleClass('mobile-nav-on');
			stopMobileNavAnim();
		});

		$('#wrap').on('click', function () {
			if (isMobileNavAnim || !$container.hasClass('mobile-nav-on')) return;

			$container.removeClass('mobile-nav-on');
		});


		$(window).scroll(function () {
			if ($(window).width() >= '767') {
				if ($(window).scrollTop() >= $(window).height()) {
					$('.sao-sidebar-box').addClass('sidebar-fixed')
					$('#header-inner').addClass('header-inner-fixed')
				} else {
					//背景毛玻璃
					let bgStyle = ($(window).scrollTop() / $(window).height()).toFixed(1) * 8
					$('#banner').css('filter', 'blur(' + bgStyle + 'px)')
					$('.sao-sidebar-box').removeClass('sidebar-fixed')
					$('#header-inner').removeClass('header-inner-fixed')
				}
			}
			if ($(window).scrollTop() >= 200) {
				$('#totop').css('display', 'flex');
			} else {
				$('#totop').css('display', 'none');
			}
		})

		//返回顶部
		$('#totop').click(function () {
			document.documentElement.scrollIntoView({
				behavior: "smooth"
			});
		})
		new Valine({
			el: '#vcomments',
			appId: 'ujOioNqbFKJJsHM0wRdmfL3E-gzGzoHsz',
			appKey: 'nICweJHFRiT20WTQIsOEP8NX',
			visitor: true,
			// enableQQ: true
		})
		$('.tocenter').click(function () {
			$('html,body').animate({ scrollTop: $('#more').offset().top }, 500);
		})
	// }
	//定位文章

	// jquery pjax
	// $('a').click(function () {
	// 	$(document).pjax('a', '#container', { fragment: '#container', timeout: 8000 });
	// })


	// }
	// $(document).ready(load_doc);

	// $(document).on('pjax:complete', function () {
	// 	load_doc();
	// 	// new Valine({
	// 	// 	el: '#vcomments',
	// 	// 	appId: 'ujOioNqbFKJJsHM0wRdmfL3E-gzGzoHsz',
	// 	// 	appKey: 'nICweJHFRiT20WTQIsOEP8NX',
	// 	// 	visitor: true,
	// 	// 	// enableQQ: true
	// 	// })
	// });

	// $(document).on('ready pjax:end', function (event) {
	// 	console.log(event)
	// 	load_doc();
	// })

})(jQuery);

new Valine({
	el: '#vcomments',
	appId: 'ujOioNqbFKJJsHM0wRdmfL3E-gzGzoHsz',
	appKey: 'nICweJHFRiT20WTQIsOEP8NX',
	visitor: true,
	// enableQQ: true
})


